/**
 * Created by JetBrains WebStorm.
 * User: Silence
 * Date: 3/25/12
 * Time: 1:12 PM
 * To change this template use File | Settings | File Templates.
 */

Ext.define('EM.controller.event.EventBudgetViewController', {
    extend: 'Ext.app.Controller',
    views: ['event.EventBudgetView', 'event.SourcePanel'],
    init: function() {
        controller = this;
        this.control({
            '#budgettreepanel': {
                itemclick: this.updateItemDetail,
                itemcontextmenu: this.showContextMenu
            },
            '#addsourcebutton': {
                click: function() {
                    var pid = Ext.getCmp('budgettreepanel').getSelectionModel().getLastSelected().get('id');
                    Ext.getCmp('itemsources').store.add({
                        name: '',
                        description: '',
                        cost: 0,
                        pid: pid
                    });
                }
            },
            '#removesourcesbutton': {
                click: this.doDelete
            }
        });
    },
    doDelete: function() {
        Ext.MessageBox.confirm('Message', 'Do you really want to delete source?' , this.doRealDelete);
    },
    doRealDelete: function(btn) {
        if (btn == 'yes') {
            var grid = Ext.getCmp('itemsources');
            var store = grid.getStore();
            store.remove(grid.getSelectionModel().getSelection());
        }
    },
    updateItemDetail: function(view, record, item, index, e, eOpts) {
        //if (record.data.leaf) {
        Ext.getCmp('itemdetailpanel').show();

        var view = Ext.getCmp('itemdetailsform');
        if (view.hidden) view.show();
        view.loadRecord(record);

        var source = Ext.getCmp('itemsources');
        source.updateStore(record.data.id, record.data.source);
        //}
    },
    showContextMenu: function(view, record, item, index, e, eOpts) {
        e.preventDefault();
        var status = record.data.status;
        var fid = record.data.id;

        Ext.create('Ext.menu.Menu', {
            items:[
                {
                    text: 'Add folder',
                    listeners: {
                        click: function () {
                            //controller.addFolder(record);
                        }
                    }
                },
                {
                    text: 'Add item',
                    listeners: {
                        click: function () {
                            //controller.addFolder(record);
                        }
                    }
                },
                {
                    text: 'Delete',
                    listeners: {
                        click: function () {
                            //controller.removeFolder(record);
                        }
                    }
                }
            ]
        }).showAt(e.getXY());
    }
});